package com.beetlestech.nfe.main.biz.com.impl;

import java.util.List;

import com.beetlestech.nfe.fw.core.biz.AbstractDBLogic;
import com.beetlestech.nfe.main.biz.com.HbSaveTeacher;
import com.beetlestech.nfe.main.biz.com.dto.TeacherInfoDTO;
import com.beetlestech.nfe.main.biz.constant.CommonConstant;
import com.beetlestech.nfe.main.biz.constant.CtgCodeConstant;
import com.beetlestech.nfe.main.biz.constant.HbMessageCode;
import com.beetlestech.nfe.main.db.constant.SQLConstant;
import com.beetlestech.nfe.main.db.constant.SQLID;
import com.beetlestech.nfe.main.db.tbl.MstcatagorycDTO;
import com.beetlestech.nfe.main.db.tbl.MstteacherDTO;
import com.beetlestech.nfe.main.db.tbl.MstusrDTO;
import com.beetlestech.nfe.main.db.tbl.TeachercourseDTO;

public class HbSaveTeacherImpl extends AbstractDBLogic
		implements HbSaveTeacher, CtgCodeConstant, HbMessageCode, CommonConstant, SQLConstant {
	protected static final String SH_INFO = "教师信息表";

	// 新增教师信息
	@Override
	public void SaveTeacherBaseInfo(String username, String password, String name, Integer[] arrtypep,
			Integer[] arrtypec) {
		// 向用户表插入信息
		MstusrDTO userDTO = new MstusrDTO();
		userDTO.setusername(username);
		userDTO.setpassword(password);
		// 教师TYPE
		userDTO.settype(2);
		userDTO.setstate(1);
		userDTO.setfirstloginflg(0);
		// 对DB执行操作
		updateDAO.updateOne(MstusrDTO.INSERT, userDTO, M10190WS, SH_INFO);
		// 向教师表插入信息
		MstteacherDTO teacherDTO = new MstteacherDTO();
		// 学生TYPE
		teacherDTO.setusrid(userDTO.getusrid());
		teacherDTO.setname(name);
		// 对DB执行操作
		updateDAO.updateOne(MstteacherDTO.INSERT, teacherDTO, M10190WS, SH_INFO);
		int tid = teacherDTO.getusrid();
		// 向课程类型插入信息
		// 插入之前先删除原有数据
		TeachercourseDTO tcDTO = new TeachercourseDTO();
		tcDTO.setteacherid(tid);
		updateDAO.update(SQLID.TEACHER_COURSETY_DELETE_BYTID, tcDTO);

		for (int i = 0; i < arrtypec.length; i++) {

			tcDTO.setteacherid(tid);
			tcDTO.setcoursetypep(arrtypep[i]);
			tcDTO.setcoursetypec(arrtypec[i]);
			if (tcDTO.getcoursetypec() != null && tcDTO.getcoursetypep() != null) {
				updateDAO.updateOne(TeachercourseDTO.INSERT, tcDTO, M10190WS, SH_INFO);
			}
		}

	}

	// 修改教师信息
	@Override
	public void UpdateTeacherBaseInfo(Integer teaId, String username, String password, String name, Integer[] arrtypep,
			Integer[] arrtypec) {
		// 通过id查询到教师信息
		MstteacherDTO teacherDTO = new MstteacherDTO();
		// teacherDTO.setusrid(teaId);
		teacherDTO.setusrid(teaId);
		teacherDTO.setname(name);
		updateDAO.updateOne(MstteacherDTO.UPDATE, teacherDTO, M10190WS, SH_INFO);
		MstusrDTO userDTO = new MstusrDTO();
		userDTO.setusrid(teacherDTO.getusrid());
		userDTO = queryDAO.queryForObject(MstusrDTO.EQUAL_SEARCH, userDTO);
		userDTO.setusername(username);
		updateDAO.updateOne(MstusrDTO.UPDATE, userDTO, M10190WS, SH_INFO);
		TeachercourseDTO tcDTO = new TeachercourseDTO();
		tcDTO.setteacherid(teaId);
		updateDAO.update(SQLID.TEACHER_COURSETY_DELETE_BYTID, tcDTO);
		for (int i = 0; i < arrtypec.length; i++) {
			tcDTO.setteacherid(teaId);
			tcDTO.setcoursetypep(arrtypep[i]);
			tcDTO.setcoursetypec(arrtypec[i]);
			updateDAO.updateOne(TeachercourseDTO.INSERT, tcDTO, M10190WS, SH_INFO);
		}
	}

	// 根据Pid 查询 授课类型
	@Override
	public TeacherInfoDTO findTypeByPid(Integer pid) {
		MstcatagorycDTO mstDTO = new MstcatagorycDTO();
		TeacherInfoDTO teDTO = new TeacherInfoDTO();
		mstDTO.setcatagorypid(pid);
		List<MstcatagorycDTO> listm = queryDAO.queryForObjectList(MstcatagorycDTO.EQUAL_SEARCH, mstDTO);

		// list 转换成数组
		MstcatagorycDTO[] arr = (MstcatagorycDTO[]) listm.toArray(new MstcatagorycDTO[listm.size()]);
		teDTO.setCatagorycDTO(arr);

		return teDTO;
	}

	// 根据tid 查询获取教师信息
	@Override
	public TeacherInfoDTO findTeacherInfoByTeaId(Integer teaId) {
		// 自定义DTO
		TeacherInfoDTO teDTO = new TeacherInfoDTO();
		// 教师信息DTO
		MstteacherDTO teacherDTO = new MstteacherDTO();
		teacherDTO.setusrid(teaId);
		teacherDTO = queryDAO.queryForObject(MstteacherDTO.EQUAL_SEARCH, teacherDTO);
		// 用户信息DTO
		MstusrDTO userDTO = new MstusrDTO();
		userDTO.setusrid(teacherDTO.getusrid());
		userDTO = queryDAO.queryForObject(MstusrDTO.EQUAL_SEARCH, userDTO);
		// 授课类型DTO
		TeachercourseDTO tcDTO = new TeachercourseDTO();
		tcDTO.setteacherid(teacherDTO.getusrid());
		List<TeachercourseDTO> listj = queryDAO.queryForObjectList(TeachercourseDTO.EQUAL_SEARCH, tcDTO);
		TeachercourseDTO[] arr = (TeachercourseDTO[]) listj.toArray(new TeachercourseDTO[listj.size()]);

		teDTO.setusername(userDTO.getusername());
		teDTO.setRealname(teacherDTO.getname());
		teDTO.setPassword(userDTO.getpassword());
		teDTO.settCatagorycDTO(arr);

		return teDTO;
	}
}
